﻿using Anke.SHManage.MSSQLDAL.TJDAL;
using Microsoft.Reporting.WebForms;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace Anke.SHManage.Web.Reports
{
    public partial class IMBalancePersonalModeWebForm : System.Web.UI.Page
    {
        private string reportName;
        private string storageName;
        private string SelectReportName1;
        private string SelectReportName2;
        protected void Page_Load(object sender, EventArgs e)
        {

            if (!IsPostBack)
            {
                InitPage();
            }
            else
            {
                reportName = Convert.ToString(ViewState["reportName"]);
            }
        }
        protected void btnSearch_Click(object sender, EventArgs e)
        {
            BindReportDataSource();
        }
        private void InitPage()
        {
             this.textMonth.Text = DateTime.Now.Year + DateTime.Now.AddMonths(-1).ToString("MM");          
             reportName = Request.QueryString["ReportName"];
             ViewState["reportName"] = reportName;
             BindReportDataSource();
        }

        protected void BindReportDataSource()
        {
            string textMonth = Convert.ToString(this.textMonth.Text);
            string reportMonth = textMonth.ToString();
            //将月份转换成那个月的起始时间和结束时间
            int mm = Convert.ToInt32(textMonth.Substring(4, 2));
            int year = Convert.ToInt32(textMonth.Substring(0, 4));
            int nextMM, nextYear, lastYear, lastMM;
            if (mm == 12)
            {
                nextMM = 1;
                nextYear = year + 1;
            }
            else
            {
                nextMM = mm + 1;
                nextYear = year;
            }
            if (mm == 1)
            {
                lastMM = 12;
                lastYear = year - 1;
            }
            else
            {
                lastMM = mm - 1;
                lastYear = year;
            }
            //DateTime beginTime = Convert.ToDateTime(year.ToString() + "-" + mm.ToString() + "-1 00:00:00");
            //DateTime endTime = Convert.ToDateTime(nextYear.ToString() + "-" + nextMM.ToString() + "-1 00:00:00");

            string MaterialType = Convert.ToString(this.MaterialType.Text);

            string DeliveryStorage = Convert.ToString(this.DeliveryStorage.Text);
            if (DeliveryStorage != "")
            { storageName = new TJDAL().GetNameByStorage(DeliveryStorage); }
            else
            {
                storageName = "上海市松江区医疗急救中心";
            }
            if (reportName == "TJ_IMBalancePersonal.rdlc" && MaterialType == "")
            { SelectReportName1 = "药品器械分站个人领用明细表";
              SelectReportName2 = "药品器械变动汇总"; 
            }
            if (reportName == "TJ_IMBalancePersonal.rdlc" && MaterialType=="MaterialType-9998")
            { SelectReportName1 = "药品器械分站个人领用明细表";
              SelectReportName2 = "药品器械变动汇总";
            }
            if (reportName == "TJ_IMBalancePersonal.rdlc" && MaterialType == "MaterialType-9999")
            { SelectReportName1 = "汽车配件分站个人领用明细表";
              SelectReportName2 = "药品器械变动汇总"; 
            }
            if (reportName == "TJ_IMBalanceYPQXBDHZ.rdlc" && MaterialType == "")
            { SelectReportName2 = "药品器械变动汇总";
              SelectReportName1 = "药品器械分站个人领用明细表";
            }
            if (reportName == "TJ_IMBalanceYPQXBDHZ.rdlc" && MaterialType == "MaterialType-9998")
            { SelectReportName2 = "药品器械变动汇总";
              SelectReportName1 = "药品器械分站个人领用明细表";
            }
            if (reportName == "TJ_IMBalanceYPQXBDHZ.rdlc" && MaterialType == "MaterialType-9999")
            { SelectReportName2 = "汽车配件变动汇总";
              SelectReportName1 = "药品器械分站个人领用明细表";
            }
            this.ReportViewer1.LocalReport.DataSources.Clear();
            this.ReportViewer1.LocalReport.ReportPath = Server.MapPath(@"~/Reports/" + reportName);
            Microsoft.Reporting.WebForms.ReportParameter month = new Microsoft.Reporting.WebForms.ReportParameter("ReportMonth", reportMonth);
            Microsoft.Reporting.WebForms.ReportParameter storage = new Microsoft.Reporting.WebForms.ReportParameter("Storage", storageName);
            Microsoft.Reporting.WebForms.ReportParameter selectReport1 = new Microsoft.Reporting.WebForms.ReportParameter("ReportName1", SelectReportName1);
            Microsoft.Reporting.WebForms.ReportParameter selectReport2 = new Microsoft.Reporting.WebForms.ReportParameter("ReportName2", SelectReportName2);
            this.ReportViewer1.LocalReport.SetParameters(new Microsoft.Reporting.WebForms.ReportParameter[] { month, storage, selectReport1, selectReport2 });
            TJDAL dal = new TJDAL();
            switch (reportName)
            {
                case "TJ_IMBalancePersonal.rdlc":
                    this.Page.Title = "药品器材分站领用明细表";
                    DataTable dt1 = dal.Get_BalancePersonal(MaterialType, DeliveryStorage, textMonth);
                    this.ReportViewer1.LocalReport.DataSources.Add(new ReportDataSource("DataSetForTJ_BalancePersonal", dt1));
                    break;
                case "TJ_IMBalanceYPQXBDHZ.rdlc":
                    this.Page.Title = "药品器材变动汇总";
                    DataTable dt2 = dal.Get_BalanceStationPerson(MaterialType, DeliveryStorage, textMonth);
                    this.ReportViewer1.LocalReport.DataSources.Add(new ReportDataSource("DataSetForTJ_BlanceYPQXBDHZ", dt2));
                    break;
                default:
                    break;
            }
            this.ReportViewer1.LocalReport.Refresh();
        }

    }
}